//
//  ContactViewController.m
//  Cai8
//
//  Created by zhoubo on 2017/6/19.
//  Copyright © 2017年 zhoubo. All rights reserved.
//

#import "ContactViewController.h"

#import <MessageUI/MessageUI.h>

@interface ContactViewController ()<MFMailComposeViewControllerDelegate>

@end

@implementation ContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    if (mailClass != nil)
    {
        // We must always check whether the current device is configured for sending emails
        if ([mailClass canSendMail])
        {
            [self sendFeedBackMail];
        }
        else
        {
            [self alertController];
        }
    }
    

}

- (void)alertController
{
    // 危险操作:弹框提醒
    // 1.UIAlertView
    // 2.UIActionSheet
    // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"邮箱未设置" message:@"请先设置您的邮箱,或者自行联系2497992599@qq.com." preferredStyle:UIAlertControllerStyleAlert];
    
    // 添加按钮
    __weak typeof(alert) weakAlert = alert;
    [alert addAction:[UIAlertAction actionWithTitle:@"了解" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //        NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"取消", @"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //        NSLog(@"点击了取消按钮");
    }]];
    
    //    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    //        NSLog(@"点击了其它按钮");
    //    }]];
    
    //    // 添加文本框
    //    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //        textField.textColor = [UIColor redColor];
    //        textField.text = @"123";
    //        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
    //        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    //    }];
    //    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //        textField.secureTextEntry = YES;
    //        textField.text = @"123";
    //    }];
    
    [self presentViewController:alert animated:YES completion:nil];
    [[NSUserDefaults standardUserDefaults] setObject:@1 forKey:@"alert"];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = false;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)sendFeedBackMail{
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"subjbect"];
    picker.navigationBar.tintColor = [UIColor redColor];
    NSArray *toRecipients = [NSArray arrayWithObject:@"1437955342@qq.com"];
    [picker setToRecipients:toRecipients];
    NSString *emailBody = [NSString
                           stringWithFormat:@"SystemVersion: %@ \n AppVersion: %@ \n\n 反馈信息:\n",                                           [[UIDevice currentDevice] systemVersion],
                           [[[NSBundle mainBundle] infoDictionary]
                            objectForKey:@"CFBundleShortVersionString"]];
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error{
    NSString *title = @"邮件发送提醒";
    NSString *msg;
    switch (result){
        case MFMailComposeResultCancelled:
            msg = @"邮件已被取消";
            break;
        case MFMailComposeResultSaved:
            msg = @"邮件保存成功";
            [self alertWithTitle:title msg:msg];
            break;
        case MFMailComposeResultSent:
            msg = @"邮件发送成功";
            [self alertWithTitle:title msg:msg];
            break;
        case MFMailComposeResultFailed:
            msg =@"邮件发送失败";
            [self alertWithTitle:title msg:msg];
            break;
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void) alertWithTitle: (NSString *)_title_ msg: (NSString *)msg{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"好"
                                          otherButtonTitles:nil];
    [alert show];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
